#pragma once
#include "stdafx.h"
#include "one.h"



#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "time.h"


////MOTION///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// various tracking parameters (in seconds)
const double MHI_DURATION = 0.01;
const double MAX_TIME_DELTA = 0.1;
const double MIN_TIME_DELTA = 0.05;
// number of cyclic frame buffer used for motion detection
// (should, probably, depend on FPS)
const int N = 2;

// ring image buffer
IplImage **buf = 0;
int last = 0;



// temporary images
IplImage *mhi = 0; // MHI
IplImage *orient = 0; // orientation
IplImage *mask = 0; // valid orientation mask
IplImage *segmask = 0; // motion segmentation map
CvMemStorage* storage = 0; // temporary storage

// parameters:
//  img - input video frame
//  dst - resultant motion picture
//  args - optional parameters



///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void  update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/1000.; // get current time in seconds
CvSize size = cvSize(img->width,img->height); // get current frame size
int i, idx1 = last, idx2;
IplImage* silh;





// allocate images at the beginning or
// reallocate them if the frame size is changed
if( !mhi || mhi->width != size.width || mhi->height != size.height ) {
if( buf == 0 ) {
buf = (IplImage**)malloc(N*sizeof(buf[0]));
memset( buf, 0, N*sizeof(buf[0]));
}
        
for( i = 0; i < N; i++ ) {
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
cvZero( buf[i] );
}
cvReleaseImage( &mhi );
cvReleaseImage( &orient );
cvReleaseImage( &segmask );
cvReleaseImage( &mask );
        
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
cvZero( mhi ); // clear MHI at the beginning
orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
}

cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale

idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;

silh = buf[idx2];
cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames

cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it

cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI

// convert MHI to blue 8u image
cvCvtScale( mhi, mask, 255./MHI_DURATION,
(MHI_DURATION - timestamp)*255./MHI_DURATION );
cvZero( dst );
cvCvtPlaneToPix( mask, mask, mask, 0, dst );



 
if( !storage )
storage = cvCreateMemStorage(0);
else
cvClearMemStorage(storage);
    


cvResetImageROI( mhi );
cvResetImageROI( orient );
cvResetImageROI( mask );
cvResetImageROI( silh );


}


/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
